<?php
/**
 * Created by PhpStorm.
 * User: 86173
 * Date: 2020-06-14
 * Time: 15:30
 */
namespace framework\libs\page;
class Page
{
    static protected $now_page;
    static protected $data_count;// 一共多少条数据
    static protected $page_data_num = 3; //一页最多多少页数据
    static protected $page_count;//总页数
    static protected $max_length = 3;

    static public function get_data($data,$now_page = 1)
    {
        self::$now_page = $now_page;
        //数据总数
        self::$data_count = count($data);
        //平均每页面多少数据
        self::$page_count = ceil(self::$data_count / self::$page_data_num);
        //判断所在页面是否存在
        if (self::$now_page > self::$page_count || self::$now_page <= 0) self::$now_page = 1;
        //获取数据的开始位置
        $data_start = self::$page_data_num * (self::$now_page - 1);
        //截取数据
        return array_slice($data, $data_start, self::$page_data_num);
    }

    static public function get_html($search="",$city_str = "")
    {
        $padding = ( self::$max_length - 1 ) / 2;
        $for_start = self::$now_page - $padding < 1 ? 1:self::$now_page - $padding;
        $for_end = self::$now_page + $padding > self::$page_count ? self::$page_count : self::$now_page + $padding;
        $html = "";
        if(self::$now_page != 1){
            $temp = self::$now_page - 1;
            $html .= "<a href = './index.php?a=index&c=index&f=index&page=1&search={$search}{$city_str}'>首页</a>&nbsp&nbsp";
            $html .="<a href = './index.php?a=index&c=index&f=index&page={$temp}&search={$search}{$city_str}'>上一页</a>&nbsp&nbsp";
        }else{
            $html .= "<span>首页</span>&nbsp&nbsp";
            $html .="<span>上一页</span>&nbsp&nbsp";
        }
        for( $i = $for_start; $i <= $for_end; $i++ ){
            if(self::$now_page == $i){
                $html .= "<span>{$i}</span>";
            }else{
                $html .= "<a href = './index.php?a=index&c=index&f=index&page={$i}&search={$search}{$city_str}'>{$i}</a>";
            }
        }
        if(self::$now_page != self::$page_count){
            $temp = self::$page_count;
            $now_page = self::$now_page + 1;
            $html .="<a href = './index.php?a=index&c=index&f=index&page={$now_page}&search={$search}{$city_str}'>下一页</a>&nbsp&nbsp";
            $html .= "<a href = './index.php?a=index&c=index&f=index&page={$temp}&search={$search}{$city_str}'>尾页</a><hr>";
        }else{
            $html .="<span>下一页</span>&nbsp&nbsp";
            $html .= "<span>尾页</span><hr>";
        }
        return $html;
    }
}
